<?php
/**************************************************
*  Created:  2010-06-08
*
*  框架初始化文件
*
*  @Xweibo (C)1996-2099 SINA Inc.
*  @Author xionghui <xionghui1@staff.sina.com.cn>
*
***************************************************/
require_once 'base.lib.php';
require_once dirname(__FILE__).'/../user_config.php';
require_once 'cfg.php';
require_once dirname(__FILE__).'/../config.php';
require_once 'compat.php';
require_once 'core.php';
//----------------------------------------------------------------------
/// 初始化全局数据
$GLOBALS[V_GLOBAL_NAME] = array();
$GLOBALS[V_GLOBAL_NAME]['TPL'] 	= array();
$GLOBALS[V_GLOBAL_NAME]['LANG'] = array();
$GLOBALS[V_GLOBAL_NAME]['PAGELETS'] = array();
$GLOBALS[V_GLOBAL_NAME]['STATIC_STORE'] = array();

/// 初始化可通过 V('-:****'); 访问的部分变量
$GLOBALS[V_CFG_GLOBAL_NAME]['userConfig']	= array();
$GLOBALS[V_CFG_GLOBAL_NAME]['sysConfig']	= array();
$GLOBALS[V_CFG_GLOBAL_NAME]['session']		= array();
$GLOBALS[V_CFG_GLOBAL_NAME]['siteInfo']		= array('site_name'=>'Nosite', 'site_uid'=>'','site_uname'=>'','reg_url'=>'', 'login_url'=>'');
//----------------------------------------------------------------------

//检查xhprof开关,并设置
//sae: sae_xhprof_start sae_xhprof_end
//common : xhprof_enable 
if ( DEBUG_XHPROF  && 1 == rand(1, DEBUG_XHPROF) || (isset($_COOKIE['debug']) && $_COOKIE['debug']) ) {
	if (function_exists('sae_xhprof_start')) {
        sae_xhprof_start();
        register_shutdown_function('sae_xhprof_end');
    }elseif (function_exists('xhprof_enable')) {
        xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);        
        function xhprof_save_result() {
            $xhprof_data = xhprof_disable();

            include_once "xhprof_lib/utils/xhprof_lib.php";
            include_once "xhprof_lib/utils/xhprof_runs.php";
            
            $xhprof_runs = new XHProfRuns_Default();

	        $page = str_replace(array('/', '.'), array('-', ':'), APP::getRequestRoute());

	        $run_id = str_replace('.','',  microtime(true)) . '.'
		    . str_replace('.', '_', getenv("REMOTE_ADDR")) . '.'
		    . $page . '.'  
		    . $xhprof_data['main()']['wt'];

            $xhprof_runs->save_run($xhprof_data, "ziyou", $run_id);
        }
        register_shutdown_function('xhprof_save_result');        
    } 
    
}

/// 与　FLASH　同步会话,让 FLASH 传递 PHPSESSID
if (defined('V_FLASH_PHPSESSID') && V(V_FLASH_PHPSESSID,false) ){
	session_id(V(V_FLASH_PHPSESSID));
}
//----------------------------------------------------------------------
///session 存储方式
if (defined('SESSION_ADAPTER') && SESSION_ADAPTER && SESSION_ADAPTER != 'default') {
	$session_adapter = APP::adapter('session', SESSION_ADAPTER);
	session_set_save_handler(array($session_adapter, "open"), array($session_adapter, "close"), array($session_adapter, "read"), array($session_adapter, "write"), array($session_adapter, "destroy"), array($session_adapter, "gc"));
}

if (defined('IS_SESSION_START') && IS_SESSION_START ){
	if(defined('ENTRY_SCRIPT_NAME') && ENTRY_SCRIPT_NAME == 'wap' && (!isset($_COOKIE) || empty($_COOKIE))){
		APP::session_wap_init();
	}else{
		session_start();
	}
}
//上报准备
APP::O('report')->prepare();
//----------------------------------------------------------------------

//----------------------------------------------------------------------
